package com.itheima.tliasproject.service.impi;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tliasproject.mapper.StudentMapper;
import com.itheima.tliasproject.pojo.PageResult;
import com.itheima.tliasproject.pojo.StuOPtion;
import com.itheima.tliasproject.pojo.Student;
import com.itheima.tliasproject.pojo.StudentQueryParam;
import com.itheima.tliasproject.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StudentServerImpI implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    //分页查询
    @Override
    public PageResult getAll(StudentQueryParam studentQueryParam) {
        //使用插件进行分页操作
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        //调用mapper
      List<Student> liststu =  studentMapper.getall(studentQueryParam);
        Page p = (Page) liststu;
        PageResult pageResult = new PageResult(p.getTotal(),p.getResult());
        return pageResult;
    }

    //删除数据
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    //新增学生信息
    @Override
    public void save(Student student) {
        //补全字段
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        studentMapper.save(student);
    }

    //根据ID查询学生数据
    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    //修改学员信息
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        student.setCreateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    //违纪情况处理
    @Override
    public void updateByscore(Integer id, Integer score) {
        if (score < 0  ){
            throw new RuntimeException("输入的违纪分数不对");
        }
        studentMapper.updateByscore(id,score);
    }





}
